#  Copyright (c) 1997-2010
#  Ewgenij Gawrilow, Michael Joswig (Technische Universitaet Darmstadt, Germany)
#  http://www.polymake.de
#
#  This program is free software; you can redistribute it and/or modify it
#  under the terms of the GNU General Public License as published by the
#  Free Software Foundation; either version 2, or (at your option) any
#  later version: http://www.gnu.org/licenses/gpl.txt.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#-------------------------------------------------------------------------------
#  $Project: polymake $$Id: formatting_filters 9562 2010-03-12 18:52:58Z smoeser $


# @category Formatting
# Prepares a matrix for printing, prepends each row with a label and a colon.
# @param Matrix data  to be printed
# @param Array<String>  row_labels labels for the rows
# @param Array<String> elem_labels  optional labels for elements;
#  if //data// is an [[IncidenceMatrix]], [[Array<Set>]], or similar, each element will be replaced by its label.
# @return Array<String> each string ending with end-of-line

user_function rows_labeled($;$$) {
   my ($data, $row_labels, $elem_labels)=@_;
   if (defined $row_labels) {
      is_array($row_labels) or croak( "row_labels must be an array" );
   }
   if (defined $elem_labels) {
      is_array($elem_labels) or croak( "elem_labels must be an array" );
   }
   my $i=-1;
   map {
      ++$i;
      (defined($row_labels) ? $row_labels->[$i] : $i) . ":" . join(" ", defined($elem_labels) ? @$elem_labels[@$_] : @$_) . "\n"
   } @$data;
}

# @category Formatting
# Like above, but especially for Graphs (defined for convenience: a PTL Graph is not a container)
# @param Graph graph to be printed
# @param Array<String> elem_labels labels for the elements

user_function rows_labeled(Graph;$) {
   my ($graph, $labels)=@_;
   rows_labeled(adjacency_matrix($graph), $labels, $labels);
}

# @category Formatting
# Equivalent to [[rows_labeled]] with omitted //row_labels// argument.
# Formerly called "numbered".
# @param Matrix data to be printed

user_function rows_numbered {
   &rows_labeled;
}


# Local Variables:
# mode: perl
# c-basic-offset:3
# End:
